package com.zkthink.ceres.app.product.controller;

import com.zkthink.base.R;
import com.zkthink.ceres.common.annotations.LoginMode;
import com.zkthink.ceres.product.dto.*;
import com.zkthink.ceres.product.entity.SearchHistory;
import com.zkthink.ceres.product.entity.ShippingCompany;
import com.zkthink.ceres.product.service.ProductGroupService;
import com.zkthink.ceres.product.service.ShippingCompanyService;
import com.zkthink.ceres.product.vo.*;
import com.zkthink.context.BaseContextHandler;
import com.zkthink.ceres.product.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("/product")
@AllArgsConstructor
@LoginMode(strict = false)
@Api(value = "商品接口", tags = "商品接口")
public class ProductController {

    private ProductService productService;

    private ProductGroupService productGroupService;

    private ShippingCompanyService shippingCompanyService;

    @GetMapping({"/queryProductDetail/{id}"})
    @ApiOperation("查询商详")
    public R<ProductVO> queryProductDetail(@PathVariable Long id) {
        return productService.queryProductDetail(id);
    }

    @PostMapping("/searchProduct")
    @ApiOperation("搜索商品")
    public R<ProductPageVO> searchProduct(@RequestBody SearchProductDTO searchProductDTO){
        Long mid = BaseContextHandler.getUserId();
        searchProductDTO.setMid(mid);
        return productService.searchProduct(searchProductDTO);
    }

    @PostMapping("/queryProductSku")
    @ApiOperation("查询商品sku")
    public R<ProductSkuVO> queryProductSku(@RequestBody QueryProductSkuDTO queryProductSkuDTO){
        return productService.queryProductSku(queryProductSkuDTO);
    }

    @ApiOperation("搜索店铺的商品")
    @PostMapping("/findAdminStoreProductList")
    public R<ProductPageVO> findAdminStoreProductList(@RequestBody AdminStoreProductDTO adminStoreProductDTO) {
        return productService.findAdminStoreProductList(adminStoreProductDTO);
    }

    @ApiOperation("查询类目商品或根据商品id列表查询商品")
    @PostMapping("findProductListByCategoryOrProductIdList")
    @LoginMode(strict = false)
    public R<CategoryProductCombineVO> findProductListByCategoryOrProductIdList(@RequestBody CategoryProductCombineDTO combineDTO){
        return productService.findProductListByCategoryOrProductIdList(combineDTO);
    }

    @ApiOperation("查询最近的N条历史记录")
    @GetMapping("/findSearchHistory")
    public R<List<SearchHistory>> findSearchHistory(@RequestParam("nCount") Integer nCount){
        if(nCount==null){
            nCount = 10;
        }
        Long mid = BaseContextHandler.getUserId();
        return productService.findLastNSearchHistory(mid, nCount);
    }

    @ApiOperation("删除某一条搜索历史")
    @PostMapping("/delSearchHistory")
    public R<Boolean> delSearchHistory(@RequestBody SearchHistory searchHistory){
        if(searchHistory!=null && StringUtils.isNotBlank(searchHistory.getKeyword())){
            Long mid = BaseContextHandler.getUserId();
            return productService.delSearchHistory(mid, searchHistory.getKeyword());
        }
        return R.success(false);
    }

    @ApiOperation("查询商家维护的商品分组")
    @PostMapping("/findProductGroupList")
    public R<List<ProductGroupVO>> findProductGroupList(@RequestBody IdQueryDTO idQueryDTO){
        return productGroupService.findProductGroupList(idQueryDTO.getId());
    }

    @ApiOperation("查询快递公司列表")
    @GetMapping(value = {"/findShippingCompanyList/{shippingCompanyName}","/findShippingCompanyList"})
    public R<List<ShippingCompany>> findShippingCompanyList(@PathVariable(value = "shippingCompanyName", required = false) String shippingCompanyName){
        return R.success(shippingCompanyService.findShippingCompanyList(shippingCompanyName));
    }
}
